/**
 * @file   Bundle.java
 * @author Enrique Madrid <e.madrid@lpcsite.com>
 * @date   Mon Sep 17 11:01:49 2007
 * 
 * @brief  
 * 
 * 
 */

package sim.bundle;

import java.util.Vector;
import java.util.Enumeration;


public final class Bundle {

    public static final int FLAG_TAIL = 0x1;
    public static final int FLAG_EDIT = 0x2;

    private Vector m_Vector = null;
    private Object m_Object = null;
    private int m_Flags = 0;

    private Bundle() {
    }

    public static Bundle create(Vector v, int flags) {
	Bundle b = null;
	if (v != null) {
	    b = new Bundle();
	    b.setVector(v);
	    b.setFlags(flags);
	}
	return b;
    }

    public static Bundle create(Vector v) {
	return create(v, 0);
    }

    protected void reset() {
	m_Object = null;
    }

    protected void insert() {
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::insert() ENTERED");
	//#endif

	// Si existe un elemento en edición ...
	if (m_Object != null) {
	    // ... y si el elemento es nuevo ...
	    if (!m_Vector.contains(m_Object)) {
		// ... y si el vector está vacío o se insertan al final.
		if ((m_Vector.isEmpty()) || ((m_Flags & FLAG_TAIL) > 0)) {
		    //#ifdef DEBUG_BUNDLE
		    //# System.out.println("\tAñadiendo elemento al final");
		    //#endif
		    m_Vector.addElement(m_Object);
		}
		else {
		    //#ifdef DEBUG_BUNDLE
		    //# System.out.println("\tAñadiendo elemento al principio");
		    //#endif
		    m_Vector.insertElementAt(m_Object, 0);
		}
	    }
	    m_Object = null;
	}

	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::insert() EXITING");
	//#endif
    }

    protected void remove(Object item) {
	if (item != null) {
	    m_Vector.removeElement(item);
	}
    }

    protected boolean select(Object item) {
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::select() BUNDLE");
	//#endif
	// Si el objeto corresponde al vector ...
	if (item != null) {
	    if (m_Vector.contains(item)) {
		// ... seleccionarlo.
		m_Object = item;
		return true;
	    }
	}
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::select() EXITING");
	//#endif
	return false;
    }

    protected int getId(Object item) {
	int ret;
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::getId() ENTERED");
	//#endif
	ret = (m_Vector.size() + 1);
	if (item != null) {
	    if (m_Vector.contains(item)) {
		//#ifdef DEBUG_BUNDLE
		//# System.out.println("\tItem pertenece al vector");
		//#endif
		ret = m_Vector.indexOf(item);
	    }
	}

	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::getId() EXITING");
	//#endif
	return ret;
    }


    protected Object getItem(int id) {
	Object item = null;
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::getItem() ENTERED");
	//#endif
	if ((id > -1) && (id < m_Vector.size())) {
	    item = m_Vector.elementAt(id);
	}
	//#ifdef DEBUG_BUNDLE
	//# System.out.println("Bundle::getItem() EXITING");
	//#endif
	return item;
    }

    /** 
     * Establece el objeto que va ha ser insertado al vector por ModBundle.
     * 
     * @param item Referencia a un objeto.
     */
    public void set(Object item) {
	if (m_Object == null) {
	    m_Object = item;
	}
    }

    /** 
     * Devuelve el objeto que va a ser insertado o ha sido seleccionado por ModBundle.
     * 
     * @return Devuelve una referencia a un objeto.
     */
    public Object get() {
	return m_Object;
    }

    public Enumeration elements() {
	return m_Vector.elements();
    }

    public void setVector(Vector v) {
	if (v != null) {
	    m_Object = null;
	    m_Vector = v;
	}
    }

    public Vector getVector() {
	return m_Vector;
    }

    public void setFlags(int flags) {
	m_Flags = flags;
    }

    public int getFlags() {
	return m_Flags;
    }
}
